// src/stores/modules/social/social.ts
import { defineStore } from 'pinia';
import { getRecommendations, getFriendRequests ,getMyFriends } from '@/api/social';
import type { RecommendUser, FriendRequest , UserDto} from '@/api/types';


// 定义社交模块的状态和方法
export const useSocialStore = defineStore('social', {
  state: () => ({
    recommendations: [] as RecommendUser[],
    friendRequests: [] as FriendRequest[],
    friends: [] as UserDto[], // 好友列表
    isLoading: false,
    error: null as string | null
  }),
  
  actions: {
    // 获取推荐用户列表
    async fetchRecommendations(limit: number , userId: number) {
      this.isLoading = true;
      this.error = null;
      try {
        this.recommendations = await getRecommendations(limit, userId);
      } catch (err: any) {
        this.error = err.message || '获取推荐失败';
      } finally {
        this.isLoading = false;
      }
    },

    // 获取好友请求列表
    async fetchFriendRequests(userId: number) {
      this.isLoading = true;
      try {
        const response = await getFriendRequests(userId);
        console.log('获取好友请求:', response);
        
        this.friendRequests = response.data || []; // 确保正确赋值data字段
      } catch (err) {
        console.error('获取好友请求失败:', err);
      } finally {
        this.isLoading = false;
      }
    },

    //获取好友列表
    // async fetchFriends(userId: number) {
    //   this.isLoading = true;
    //   try {
    //     const response = await getMyFriends(userId);
    //     this.friends = response.data || [];
    //   } catch (err) {
    //     console.error('获取好友列表失败:', err);
    //   } finally {
    //     this.isLoading = false;
    //   }
    // }
  }
});